<?php
Yii::import('bootstrap.widgets.TbMenu', true);

class ProjectMenuWidget extends TbMenu
{
	public $type = TbMenu::TYPE_LIST;
	public $htmlOptions = array('class' => 'bs-docs-sidenav affix');
	public $stacked = true;

	public function init()
	{
		$user = Yii::app()->user->getModel(); /* @var $user User */
		$project = $this->getOwner()->projectName;
		$proItems = array();
		foreach (Yii::app()->pf->getAll() as $tmp) {
			if (isset($tmp['error']) || !$user->hasAccess($tmp['name'])) {
				continue;
			}
			$proItems[] = array(
				'label' => $tmp['name'],
				'url' => array('project/schema', 'project' => $tmp['name']),
			);
		}
		$items = array(
			'data/list' => '数据列表',
			'data/create' => '添加数据',
			'data/import' => '导入数据',
			'data/clean' => '清空数据',
			'---',
			'project/hot' => '热门搜索词',
			'project/log' => '搜索日志管理',
			'project/synonym' => '同义词管理',
			'project/dict' => '自定义词库',
			'---',
			'project/task' => '计划任务',
			'api/index' => 'Restful-API',
		);
		$this->items = array(
			array(
				'label' => $project,
				'icon' => 'plus-sign',
				'url' => array('project/schema'),
				'items' => $proItems,
			)
		);
		$accessList = Yii::app()->params['accessList'];
		foreach ($items as $route => $label) {
			if ($label === '---') {
				$this->items[] = $label;
			} else {
				if (isset($accessList[$route]) && !$user->hasAccess($project, $route)) {
					continue;
				}
				$this->items[] = array(
					'label' => $label,
					'icon' => 'arrow-right black',
					'url' => array($route),
				);
			}
		}
		parent::init();
	}

	protected function isItemActive($item, $route)
	{
		if (!strncmp($route, 'api/', 4) && isset($item['url']) && $item['url'][0] === 'api/index') {
			return true;
		}
		if ($route === 'data/update' && isset($item['url']) && $item['url'][0] === 'data/list') {
			return true;
		}
		if ($route === 'project/change' && isset($item['url']) && $item['url'][0] === 'project/schema') {
			return true;
		}
		return parent::isItemActive($item, $route);
	}
}
